import QtQuick 1.1

MyPage {
    id: messagesPage

    /********************************************************************************/
    colorPage: "white"

    Component {
        id: componentAccountItem
        Rectangle {
            id: wrapper
            clip: true
            width: listViewAccounts.width
            height: 90
            //color: "white"
            gradient: gr_free
            Gradient {
                id: gr_free
                GradientStop { id: gr1; position: 0; color: "white" }
                GradientStop { id: gr2; position: 0.5; color: "#ededed" }
                GradientStop { id: gr3; position: 1; color: "#d7d7d7" }
            }
            Gradient {
                id: gr_press
                GradientStop { position: 0; color: "lightblue" }
                GradientStop { position: 1; color: "lightblue" }
                GradientStop { position: 0.5; color: "#ededed" }
            }
            Image {
                id: imgAcc
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 10
                source: accIcon
            }
            Text {
                id: txtAcc
                anchors.verticalCenter: parent.verticalCenter
                anchors.left: imgAcc.right
                anchors.leftMargin: 10
                anchors.right: imgDefualt.visible ? imgDefualt.left : parent.right
                anchors.rightMargin: 10
                text: accJid
                font.pixelSize: 36
                clip: true
            }
            Image {
                id: imgDefualt
                source: "qrc:/qml/images/bar_ok.png"
                anchors.right: parent.right; anchors.rightMargin: 5
                anchors.verticalCenter: parent.verticalCenter
                //anchors.top: parent.top; anchors.topMargin: 5
                height: 40
                width: 40
                smooth: true
                visible: accDefault
            }

            states: State {
                name: "Current"
                when: (wrapper.ListView.isCurrentItem && (main.accJid != "") )
                //PropertyChanges { target: wrapper; color: "lightblue" }
                PropertyChanges { target: wrapper; gradient: gr_press }
            }

            transitions: Transition {
                //NumberAnimation { properties: "position"; duration: 300 }
            }

            MouseArea {
                id: maAccItem
                anchors.fill: parent
                onDoubleClicked: {
                    wrapper.ListView.view.currentIndex = index
                    main.accJid = accJid
                    main.accPass = accPasswd
                    main.accDefault = accDefault
                    main.accResource = accResource
                    main.accHost = accHost
                    main.accPort = accPort
                    main.accManualHostPort = accManualHostPort
                    messagesPage.closePage( "qrc:/qml/AccountAddPage.qml" )
                }
                onClicked: {
                    wrapper.ListView.view.currentIndex = index
                    main.accJid = accJid
                    main.accPass = accPasswd
                    main.accDefault = accDefault
                    main.accResource = accResource
                    main.accHost = accHost
                    main.accPort = accPort
                    main.accManualHostPort = accManualHostPort
                }
            }

        }
    }

    ListView {
        id: listViewAccounts
        anchors.top: parent.top
        anchors.bottom: toolBar.top
        anchors.left: parent.left
        anchors.right: parent.right
        clip: true
        delegate: componentAccountItem
        model: settings.accounts
    }

    Component.onCompleted: {
        settings.initListOfAccounts()
        main.accJid = ""
    }


    /********************************( Toolbar )************************************/

    ToolBar {
        id: toolBar

        ToolButton {
            id: toolBarButtonRoster
            icon: "qrc:/qml/images/bar_roster.png"
            anchors.left: parent.left
            anchors.leftMargin: (0.5*(parent.width/4) - 0.5*toolBarButtonRoster.width)
            onClicked: {
                main.initAccount() // инициализация аккуанта на клиента
                messagesPage.closePage( "qrc:/qml/RosterPage.qml" )
            }
            pauseAnim: 500
        }

        ToolButton {
            id: toolBarButtonRemove
            icon: "qrc:/qml/images/bar_close.png"
            anchors.left: parent.left
            anchors.leftMargin: (1.5*(parent.width/4) - 0.5*toolBarButtonRemove.width)
            onClicked: {
                if( main.accJid != "" ) {
                    dlgQueryRemove.dlgShowHide()
                }
            }
            pauseAnim: 600
        }

        ToolButton {
            id: toolBarButtonEdit
            icon: "qrc:/qml/images/bar_edit.png"
            anchors.left: parent.left
            anchors.leftMargin: (2.5*(parent.width/4) - 0.5*toolBarButtonEdit.width)
            onClicked: {
                if( main.accJid != "" ) {
                    messagesPage.closePage( "qrc:/qml/AccountAddPage.qml" )
                }
            }
            pauseAnim: 700
        }

        ToolButton {
            id: toolBarButtonOptions
            icon: "qrc:/qml/images/bar_add.png"
            anchors.left: parent.left
            anchors.leftMargin: (3.5*(parent.width/4) - 0.5*toolBarButtonOptions.width)
            onClicked: {
                main.accJid = ""
                main.accPass = ""
                main.accDefault = false
                main.accResource = ""
                main.accHost = ""
                main.accPort = ""
                messagesPage.closePage( "qrc:/qml/AccountAddPage.qml" )
            }
            pauseAnim: 800
        }
    }


    /*********************************************************************/
    DialogQuery {
        id: dlgQueryRemove
        title: qsTr("Remove account")
        text: qsTr("Remove ") + main.accJid + " ?"
        onClickedOk: {
            settings.removeAccount( main.accJid )
            settings.initListOfAccounts()
        }
    }

    /*********************************************************************/
}
